$(function(){
    $(".select-commonDict").each(function(){
        //获取当前select的jquery包装对象
        var $this = $(this);
        //获取该select的要设置的默认值
        var defualtValue = $this.attr("defualt-value");
        //获取该select的要显示的默认值
        var defualtShow = $this.attr("defualt-show") ? $this.attr("defualt-show") : "--请选择--";
        //获取该select的data-modelCode值
        var modelCode = $this.attr("data-modelCode");
        //获取该select的data-funCode值
        var funCode = $this.attr("data-funCode");
        //获取该select是否有特殊的val值段，默认为cdId
        var valueField = $this.attr("data-valueField") ? $this.attr("data-valueField") : "cdId";
        //设置查询的propertyParentCode为默认的0值
        var propertyParentCode = 0;
        //获取该select是否有级联查询的selectId，默认为空
        var cascadeSelectId = $this.attr("data-cascade") ? '#' + $this.attr("data-cascade") : "";
        //使用eazyui的combobox去获取下拉选框的值
        if(cascadeSelectId){
            var add_flag = 0;
            $this.combobox({
                url: CONTEXT_PATH +'/commonDict/getCommonDictOptions.do?modelCode='+ modelCode + '&funCode=' + funCode + '&propertyParentCode=' + propertyParentCode,
                method: 'get',
                valueField: valueField,
                textField:'propertyName',
                onSelect: function(rec){
                    //获取级联select的jquery包装对象
                    var $cascadeSelect = $(cascadeSelectId);
                    //先清空级联选框的值
                    $cascadeSelect.combobox("loadData", []);
                    //获取级联select的要设置的默认值
                    var defualtValueCS = $cascadeSelect.attr("defualt-value");
                    //获取级联select的要显示的默认值
                    var defualtShowCS = $cascadeSelect.attr("defualt-show") ? $cascadeSelect.attr("defualt-show") : "--请选择--";
                    //获取级联select的data-modelCode值
                    var modelCodeCS = $cascadeSelect.attr("data-modelCode");
                    //获取级联select的data-funCode值
                    var funCodeCS = $cascadeSelect.attr("data-funCode");
                    //获取级联select是否有特殊的val值段，默认为cdId
                    var valueFieldCS = $cascadeSelect.attr("data-valueField") ? $cascadeSelect.attr("data-valueField") : "cdId";
                    //设置查询的propertyParentCode为当前选取的id值
                    var propertyParentCode = rec.cdId ? rec.cdId : 0;
                    var url = CONTEXT_PATH +'/commonDict/getCommonDictOptions.do?modelCode='+ modelCodeCS + '&funCode=' + funCodeCS + '&propertyParentCode=' + propertyParentCode;
                    //清空级联select的下拉选项
                    $cascadeSelect.combobox("clear");
                    //设置一个级联选框加载默认值的标记
                    var add_flag_cascade = 0
                    $cascadeSelect.combobox({
                        url: url,
                        method: 'get',
                        valueField: valueFieldCS,
                        textField:'propertyName',
                        onLoadSuccess: function(){
                            if(add_flag_cascade == 0){
                                add_flag_cascade += 1;
                                var data = $cascadeSelect.combobox("getData");
                                var newDataJson = {};
                                newDataJson[valueField] = "";
                                newDataJson["propertyName"] = defualtShowCS;
                                newDataJson["selected"] = "true";
                                data.unshift(newDataJson);
                                $cascadeSelect.combobox("loadData", data);
                            }
                        }
                    });
                },
                onLoadSuccess: function(){
                    if(add_flag == 0){
                        add_flag += 1;
                        var data = $this.combobox("getData");
                        var newDataJson = {};
                        newDataJson[valueField] = "";
                        newDataJson["propertyName"] = defualtShow;
                        newDataJson["selected"] = "true";
                        data.unshift(newDataJson);
                        $this.combobox("loadData", data);
                        if(defualtValue){
                            $this.combobox("select", defualtValue);
                        }
                        //设置级联的选框的默认值
                        //获取级联select的jquery包装对象
                        var $cascadeSelect = $(cascadeSelectId);
                        //获取级联select的要设置的默认值
                        var defualtValueCS = $cascadeSelect.attr("defualt-value");
                        //获取级联select的要显示的默认值
                        var defualtShowCS = $cascadeSelect.attr("defualt-show") ? $cascadeSelect.attr("defualt-show") : "--请选择--";
                        //获取级联select的data-modelCode值
                        var modelCodeCS = $cascadeSelect.attr("data-modelCode");
                        //获取级联select的data-funCode值
                        var funCodeCS = $cascadeSelect.attr("data-funCode");
                        //获取级联select是否有特殊的val值段，默认为cdId
                        var valueFieldCS = $cascadeSelect.attr("data-valueField") ? $cascadeSelect.attr("data-valueField") : "cdId";
                        //设置查询的propertyParentCode为当前选取的id值
                        var propertyParentCode = defualtValue;
                        var url = CONTEXT_PATH +'/commonDict/getCommonDictOptions.do?modelCode='+ modelCodeCS + '&funCode=' + funCodeCS + '&propertyParentCode=' + propertyParentCode;
                        //清空级联select的下拉选项
                        $cascadeSelect.combobox("clear");
                        //设置一个级联选框加载默认值的标记
                        var add_flag_cascade = 0
                        $cascadeSelect.combobox({
                            url: url,
                            method: 'get',
                            valueField: valueFieldCS,
                            textField:'propertyName',
                            onLoadSuccess: function(){
                                if(add_flag_cascade == 0){
                                    add_flag_cascade += 1;
                                    var data = $cascadeSelect.combobox("getData");
                                    var newDataJson = {};
                                    newDataJson[valueField] = "";
                                    newDataJson["propertyName"] = defualtShowCS;
                                    newDataJson["selected"] = "true";
                                    data.unshift(newDataJson);
                                    $cascadeSelect.combobox("loadData", data);
                                    if(defualtValueCS){
                                        $cascadeSelect.combobox("select", defualtValueCS);
                                    }
                                }
                            }
                        });
                        $cascadeSelect.combobox("select", defualtValueCS);
                    }
                }
            });
        }else{
            //设置一个标记值，让onLoadSuccess和combobox()不要无限循环
            var add_flag = 0;
            $this.combobox({
                url: CONTEXT_PATH +'/commonDict/getCommonDictOptions.do?modelCode='+ modelCode + '&funCode=' + funCode + '&propertyParentCode=' + propertyParentCode,
                method: 'get',
                "valueField" : valueField,
                textField:'propertyName',
                onLoadSuccess: function(){
                    if(add_flag == 0){
                        add_flag += 1;
                        var data = $this.combobox("getData");
                        var newDataJson = {};
                        newDataJson[valueField] = "";
                        newDataJson["propertyName"] = defualtShow;
                        newDataJson["selected"] = "true";
                        data.unshift(newDataJson);
                        $this.combobox("loadData", data);
                        if(defualtValue){
                            $this.combobox("select", defualtValue);
                        }
                    }

                }
            });
        }
    });

    // 省市区下拉选择,只需在省的标签上设置class=select-region,
    // 添加级联的市与区县的id；set-cityId=“”，et-districtId=“”
    $(".select-region").each(function() {
        // 获取当前select的jquery包装对象
        var $this = $(this);
        // 获取该select的要显示的默认值
        var defualtShow = "-请选择-";
        // 获取省默认值
        var defualtValue = $this.attr("defualt-value");
        // 获取级联设置市的id
        var cityId = $this.attr("set-cityId");
        // 获取级联设置区县的id
        var districtId = $this.attr("set-districtId");
        //获取市的初始值
        var defaultCity = $('#'+cityId).combobox("getValue");
        var add_flag = 0;
        //设置默认value
        var newDataJson = {};
        newDataJson["id"] = "";
        newDataJson["text"] = defualtShow;
        newDataJson["selected"] = "true";
        // 加载省份
        $this.combobox({
            url: CONTEXT_PATH +'/region/regionList.do?type=1',
            method: 'get',
            valueField:'id',
            textField:'text',
            onSelect: function(rec){
                //选中的不是空值情况，加载市级，否则清空市与县
                if(rec.id != ""){
                    // 获取城市的选择的值
                    var cityValue = $('#'+cityId).combobox("getValue");
                    //初始化时不作清空操作
                    if(defualtValue != rec.id){
                        cityValue = "";
                        //判断连级是否存在值,有值才能清空
                        var cityData = $('#'+cityId).combobox('getData');
                        var districtData = $('#'+districtId).combobox('getData');
                        if(cityData.length > 1){
                            // 清空级联市的下拉选项
                            $('#'+cityId).combobox("clear");//清空选中项
                            $('#'+cityId).combobox("loadData", [newDataJson]);

                        }
                        if(districtData.length > 1){
                            //清空级联区县
                            $('#'+districtId).combobox("clear");
                            $('#'+districtId).combobox("loadData", [newDataJson]);
                        }
                    }
                    // 设置一个级联选框加载默认值的标记,首先级联市的下拉
                    var add_flag_city = 0;
                    $('#'+cityId).combobox({
                        url: CONTEXT_PATH +'/region/regionList.do?type=2&codeId='+rec.id,
                        method: 'get',
                        valueField: 'id',
                        textField:'text',
                        onSelect: function(rec){
                            if(rec.id != ""){
                                // 获取区县的默认值
                                var districtValue = $('#'+districtId).combobox("getValue");
                                if(defaultCity != rec.id){
                                    districtValue = "";
                                    // 清空级联区县的下拉选项
                                    var districtData = $('#'+districtId).combobox('getData');
                                    if(districtData.length > 1){
                                        //清空级联区县
                                        $('#'+districtId).combobox("clear");
                                        $('#'+districtId).combobox("loadData", [newDataJson]);
                                    }
                                };
                                // 设置一个级联选框加载默认值的标记,其次级联区县的下拉
                                var add_flag_district = 0;
                                $('#'+districtId).combobox({
                                    url: CONTEXT_PATH +'/region/regionList.do?type=3&codeId='+rec.id,
                                    method: 'get',
                                    valueField: 'id',
                                    textField:'text',
                                    onLoadSuccess: function(){
                                        if(add_flag_district == 0){
                                            add_flag_district += 1;
                                            var data = $('#'+districtId).combobox("getData");
                                            data.unshift(newDataJson);
                                            $('#'+districtId).combobox("loadData", data);
                                            if(districtValue != ""){
                                                $('#'+districtId).combobox("select", districtValue);
                                            }
                                        }
                                    }
                                });
                            }else{
                                var districtData = $('#'+districtId).combobox('getData');
                                if(districtData.length > 1){
                                    //清空级联区县
                                    $('#'+districtId).combobox("clear");
                                    $('#'+districtId).combobox('loadData', [newDataJson]);

                                }
                            }
                        },
                        onLoadSuccess: function(){
                            if(add_flag_city == 0){
                                add_flag_city += 1;
                                var data = $('#'+cityId).combobox("getData");
                                data.unshift(newDataJson);
                                $('#'+cityId).combobox("loadData", data);
                                if(cityValue != ""){
                                    $('#'+cityId).combobox("select", cityValue);
                                }
                            }
                        }
                    });
                }else{
                    //判断连级是否存在值，存在才能清空
                    var cityData = $('#'+cityId).combobox('getData');
                    var districtData = $('#'+districtId).combobox('getData');
                    if(cityData.length > 1 ){
                        // 清空级联市的下拉选项
                        $('#'+cityId).combobox("clear");//清空选中项
                        $('#'+cityId).combobox("loadData", [newDataJson]);

                    }
                    if(districtData.length > 1){
                        //清空级联区县
                        $('#'+districtId).combobox("clear");
                        $('#'+districtId).combobox("loadData", [newDataJson]);
                    }
                }
            },
            onLoadSuccess: function(){
                if(add_flag == 0){
                    add_flag += 1;
                    var data = $this.combobox("getData");
                    var newDataJson = {};
                    newDataJson['id'] = "";
                    newDataJson["text"] = defualtShow;
                    newDataJson["selected"] = "true";
                    data.unshift(newDataJson);
                    $this.combobox("loadData", data);
                    $('#'+cityId).combobox({
                        valueField:'id',
                        textField:'text'
                    });
                    $('#'+districtId).combobox({
                        valueField:'id',
                        textField:'text'
                    });
                    $('#'+cityId).combobox("loadData", [newDataJson]);
                    $('#'+districtId).combobox("loadData", [newDataJson]);
                    if(defualtValue){
                        $this.combobox("select", defualtValue);
                    }
                }
            }
        })
    })
});
